package com.idroc.coelacanth.core.membership;

import java.util.ArrayList;
import java.util.List;

import org.jboss.netty.buffer.ChannelBuffer;

import com.idroc.coelacanth.core.message.AbstractMessage;

/**
 * 端点成员响应消息
 * @author yuyoo (yuyoo4j@163.com)
 * @date Dec 6, 2009 12:23:23 PM
 */
public class MemberSetResponseMessage extends AbstractMessage {
	
	/**
	 * 消息来源端点
	 */
	private Endpoint from = null;
	
	/**
	 * 加入状态的端点集合
	 */
	private List<Endpoint> joinEndpoints = null;
	
	/**
	 * 开放状态的端点集合
	 */
	private List<Endpoint> openEndpoints = null;
	
	/**
	 * 关闭状态的端点集合
	 */
	private List<Endpoint> closeEndpoints = null;

	@Override
	public void decode(ChannelBuffer ib) {
		
		from = new Endpoint();
		from.setHost(decodeString(ib));
		from.setPort(ib.readInt()); 
		
		int size = ib.readInt();
		List<Endpoint> oe = new ArrayList<Endpoint>(size);
		for (int i = 0; i < size; i++) {
			Endpoint item = new Endpoint();
			item.setHost(decodeString(ib));
			item.setPort(ib.readInt());
			oe.add(item);
		}
		openEndpoints = oe;
		
		size = ib.readInt();
		List<Endpoint> ce = new ArrayList<Endpoint>(size);
		for (int i = 0; i < size; i++) {
			Endpoint item = new Endpoint();
			item.setHost(decodeString(ib));
			item.setPort(ib.readInt());
			ce.add(item);
		}
		closeEndpoints = ce;
		
		size = ib.readInt();
		List<Endpoint> je = new ArrayList<Endpoint>(size);
		for (int i = 0; i < size; i++) {
			Endpoint item = new Endpoint();
			item.setHost(decodeString(ib));
			item.setPort(ib.readInt());
			je.add(item);
		}
		joinEndpoints = je;
	}

	@Override
	public void encode(ChannelBuffer ob) {
		
		encodeString(ob, from.getHost());
		ob.writeInt(from.getPort());
		int size = openEndpoints.size();
		ob.writeInt(size);
		for (int i = 0; i < size; i++) {
			Endpoint item = openEndpoints.get(i);
			encodeString(ob, item.getHost());
			ob.writeInt(item.getPort());
		}
		size = closeEndpoints.size();
		ob.writeInt(size);
		for (int i = 0; i < size; i++) {
			Endpoint item = closeEndpoints.get(i);
			encodeString(ob, item.getHost());
			ob.writeInt(item.getPort());
		}
		size = joinEndpoints.size();
		ob.writeInt(size);
		for (int i = 0; i < size; i++) {
			Endpoint item = joinEndpoints.get(i);
			encodeString(ob, item.getHost());
			ob.writeInt(item.getPort());
		}
	}

	public Endpoint getFrom() {
		return from;
	}

	public void setFrom(Endpoint from) {
		this.from = from;
	}

	public List<Endpoint> getOpenEndpoints() {
		return openEndpoints;
	}

	public void setOpenEndpoints(List<Endpoint> openEndpoints) {
		this.openEndpoints = openEndpoints;
	}

	public List<Endpoint> getCloseEndpoints() {
		return closeEndpoints;
	}

	public void setCloseEndpoints(List<Endpoint> closeEndpoints) {
		this.closeEndpoints = closeEndpoints;
	}

	public List<Endpoint> getJoinEndpoints() {
		return joinEndpoints;
	}

	public void setJoinEndpoints(List<Endpoint> joinEndpoints) {
		this.joinEndpoints = joinEndpoints;
	}

	@Override
	public String toString() {

		StringBuilder sb = new StringBuilder();
		sb.append("MemberSetResponseMessage[");
		sb.append("sn:").append(this.getSN()).append(",");
		sb.append("from:").append(from).append(",");
		sb.append("openEndpoints:").append(openEndpoints).append(",");
		sb.append("closeEndpoints:").append(closeEndpoints).append(",");
		sb.append("joinEndpoints:").append(joinEndpoints);
		sb.append("]");
		return sb.toString();
	}
}
